<?php
/**
 * 聚水潭
 * @author           程顺伟
 * @lastmodify       2022-06-26
 */

defined('IN_YZMPHP') or exit('Access Denied');
new_session_start();
yzm_base::load_common('function/hr.func.php');

class hr_timer{
	
	//每天凌晨处理
	public function calc_money_list(){
		//已离职人，薪资变动申请，不做处理（待优化）
		$time = strtotime(date('Y-m-d'));
		$money_list = D('hr_money_list')->where(['is_action'=>2,''])->select();
		if(!$money_list){
			echo '暂无需要处理的工资明细！';
		}
		foreach($money_list as $v){
			//还未到生效时间不做处理
			if($v['action_time']>$time){
				echo 'ID:'.$v['id'].'，未到生效时间跳过！'.PHP_EOL;
				continue;
			}
			$result = D('hr_money_list')->update(['is_action'=>1],['id'=>$v['id']]);
			$result = D('hr_member')->update(['money_id'=>$v['id']],['id'=>$v['member_id']]);
			echo 'ID:'.$v['id'].'，生效成功！'.PHP_EOL;
		}
	}
	
	//每个月月初，更新上月工资id到member表
	public function update_last_money_id(){
		$member = D('hr_member')->where(['status'=>1])->select();
		foreach($member as $v){
			$result = D('hr_member')->update(['last_money_id'=>$v['money_id']],['id'=>$v['id']]);
			if($result){
				echo '员工:'.$v['name'].'，更新失败！'.PHP_EOL;
			}else{
				echo '员工:'.$v['name'].'，更新成功！'.PHP_EOL;
			}
			
		}
	}
	
	//年假重置
	public function calc_leave_list(){
		$member_list = D('hr_member')->field('id,position_id,name,in_time')->where(['status'=>1])->select();
		foreach($member_list as $v){
			$result = calc_member_leave($v);
			echo $v['name'].'：'.$result['message'].PHP_EOL;
		}
	}
}